package com.starryx.driverschool.service.impl;

import com.starryx.driverschool.dao.StudentRepository;
import com.starryx.driverschool.dao.TeacherRepository;
import com.starryx.driverschool.po.Student;
import com.starryx.driverschool.po.Teacher;
import com.starryx.driverschool.service.TeacherService;
import com.starryx.driverschool.utils.MyBeanUtils;
import com.starryx.driverschool.vo.TeacherQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public List<Teacher> listTeacher() {
        return teacherRepository.findAll();
    }

    @Override
    public Page<Teacher> listTeacher(Pageable pageable) {
        return teacherRepository.findAll(pageable);
    }

    @Override
    public Page<Teacher> listTeacher(Pageable pageable, TeacherQuery teacherQuery) {
        return teacherRepository.findAll(new Specification<Teacher>() {
            @Override
            public Predicate toPredicate(Root<Teacher> root,       // 相当于表名
                                         CriteriaQuery<?> cq,   // 条件查询执行器
                                         CriteriaBuilder cb) {  // 条件语句构建器，用于定义查询条件
                List<Predicate> predicates = new ArrayList<>();     // 查询条件组
                if (!"".equals(teacherQuery.getNo()) && teacherQuery.getNo() != null) {
                    predicates.add(cb.like(root.<String>get("no"), "%" + teacherQuery.getNo() + "%"));
                }
                if (!"".equals(teacherQuery.getType()) && teacherQuery.getType() != null) {
                    predicates.add(cb.like(root.<String>get("car").<String>get("type"), "%" + teacherQuery.getType() + "%"));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));     // 将条件构造器构建的查询条件放到执行器执行
                return null;
            }
        }, pageable);
    }

    @Transactional
    @Override
    public Teacher saveTeacher(Teacher teacher) {
        teacher.setUpdateTime(new Date());
        teacher.setCreateTime(new Date());
        return teacherRepository.save(teacher);
    }

    @Override
    public Teacher getTeacher(Long id) {
        return teacherRepository.findOne(id);
    }

    @Override
    public Teacher getTeacherByCarId(Long id) {
        return teacherRepository.findByCarId(id);
    }

    @Override
    public Teacher getTeacherByNo(String no) {
        return teacherRepository.findByNo(no);
    }

    @Transactional
    @Override
    public Teacher updateTeacher(Long id, Teacher teacher) {
        Teacher c = teacherRepository.findOne(id);
        if(c == null){
            throw new RuntimeException("不存在该类型");
        }
        BeanUtils.copyProperties(teacher, c, MyBeanUtils.getNullPropertNames(teacher));
        c.setUpdateTime(new Date());
        return teacherRepository.save(c);
    }

    @Transactional
    @Override
    public void deleteTeacher(Long id) {
        Student student = studentRepository.findByTeacherId(id);
        student.setTeacher(null);
        studentRepository.save(student);
        teacherRepository.delete(id);
    }
}
